package com.clkj.goldpartner.dao;

import com.clkj.goldpartner.pojo.InStock;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

/**
 * @author 杨行
 * @date 2021/3/3 13:42
 */
@Repository
public interface InStockMapper extends Mapper<InStock> {

    /**
     * 获取未完成订单
     *
     * @param shop_id
     * @param supplier_id
     * @param status
     * @return
     */
    @Select("<script>" +
            "select a.*,b.member_name as purchase_name from t_cl_inStock a left join t_cl_member b on a.purchase_id = b.member_id where a.shop_id = #{shop_id} " +
            "<if test=' 1==status '> and a.in_status = '0' </if> " +
            "<if test=' 2==status '> and a.in_status = '1' </if> " +
            "<if test=' 3==status '> and a.in_status in ('0','2') </if> " +
            "<if test=' 0!=supplier_id '> and a.supplier_id = #{supplier_id} </if> " +
            "</script>")
    List<Map> getInStock(Integer shop_id, Integer supplier_id, String status);

    /**
     * 更新进货订单状态
     *
     * @param in_num
     * @param in_status
     */
    @Update("update t_cl_inStock set in_status = #{in_status} where in_num = #{in_num}")
    void updateInStock(String in_num, String in_status);

    /**
     * 获取最后一位编号
     *
     * @return String
     */
    @Select("select top 1 in_num from t_cl_inStock group by inStock_id,in_num order by inStock_id desc ")
    String getIn_num();


    /**
     * 货品进货明细
     *
     * @param shop_id
     * @param goods_id
     * @param start_time
     * @param end_time
     * @return
     */
    @Select("<script>" +
            "select * from view_cl_goodsInDetail where goods_status = 0 and stock_status = 0 and shop_id = #{shop_id} " +
            "<if test='goods_id!=0'> and goods_id= #{goods_id} </if> " +
            "<if test='start_time!=\"0\"'> and step4_date &gt;= #{start_time} </if> " +
            "<if test='end_time!=\"0\"'> and step4_date &lt;= #{end_time}  </if> " +
            "</script>")
    List<Map<String, Object>> listGoodsInDetail(Integer shop_id, Integer goods_id, String start_time, String end_time);

    /**
     * 供应商供货明细
     *
     * @param shop_id
     * @param supplier
     * @param start_time
     * @param end_time
     * @return
     */
    @Select("<script>" +
            "select * from View_cl_gyGoodsDetail where goods_status = 0 and stock_status = 0 and shop_id = #{shop_id}" +
            "<if test=' supplier!=0'> and supplier_id= #{supplier} </if> " +
            "<if test=' start_time!=\"0\"'> and step4_date &gt;= #{start_time} </if> " +
            "<if test=' end_time!=\"0\"'> and step4_date &lt;= #{end_time}  </if> " +
            "</script>")
    List<Map<String, Object>> listGyDetail(Integer shop_id, Integer supplier, String start_time, String end_time);

    /**
     * 货品销售明细
     *
     * @param shop_id
     * @param goods_id
     * @param start_time
     * @param end_time
     * @param flag       flag--0货品销售明细,flag--1业务员销售明细，flag--2客户销售明细
     * @return
     */
    @Select("<script>" +
            "select * from View_cl_goodsSaleDetail where goods_status = 0 and stock_status = 1 and shop_id = #{shop_id}" +

            "<if test=' flag==\"0\"'>" +
            "<if test=' goods_id!=0'> and goods_id= #{goods_id} </if> " +
            "</if>" +
            "<if test=' flag==\"1\"'>" +
            "<if test=' goods_id!=0'> and saleman_id= #{goods_id} </if> " +
            "</if>" +
            "<if test=' flag==\"2\"'>" +
            "<if test=' goods_id!=0'> and customer_id= #{goods_id} </if> " +
            "</if>" +
            "<if test=' start_time!=\"0\"'> and step2_date &gt;= #{start_time} </if>" +
            "<if test=' end_time!=\"0\"'> and step2_date &lt;= #{end_time}  </if>" +
            "</script>")
    List<Map<String, Object>> listGoodsSaleDetail(Integer shop_id, Integer goods_id, String start_time, String end_time, String flag);

    /**
     * 商品库存明细
     *
     * @param shop_id
     * @param goods_id
     * @param warehouse_id
     * @param goods_num
     * @param category_id
     * @param flag
     * @return
     */
    @Select("<script>" +
            "select gs.*,g.goods_num,g.goods_name,g.goods_format,g.goods_unit,g.product_factory,g.registration_NO,g.storage_mode,g.curing_type,g.curing_time,g.production_NO,g.curing_type,g.curing_time,w.warehouse_name,w.warehouse_id,ca.category_id,ca.category_name,g.goods_note,g.UDI from t_cl_goodsStock  gs" +
            " left join t_cl_goods  g on gs.goods_id = g.goods_id " +
            "left join t_cl_warehouse w on gs.warehouse_id=w.warehouse_id" +
            " left join t_cl_category  ca on g.category_id=ca.category_id where gs.shop_id =  #{shop_id}" +
            " and (g.goods_num like '%${goods_num}%' or g.goods_name like '%${goods_num}%') " +
            "<if test=' flag!=1'> and gs.goods_number &gt; 0 </if> " +
            "<if test=' goods_id!=0'> and gs.goods_id= #{goods_id} </if> " +
            "<if test=' warehouse_id!=0'> and gs.warehouse_id= #{warehouse_id} </if>" +
            "<if test=' category_id!=0'> and g.category_id= #{category_id} </if>" +
            "</script> ")
    List<Map<String, Object>> listStockDetail(Integer shop_id, Integer goods_id, Integer warehouse_id, @Param("goods_num") String goods_num, Integer category_id, Integer flag);

    /**
     * 库存数量汇总表
     *
     * @param shop_id
     * @param warehouse_id
     * @return
     */
    @Select("<script>" +
            "select a.goods_number ,a.goods_id,c.stock_status,b.goods_name,b.goods_format, c.goods_number as sale_num  " +
            "from  t_cl_goodsStock  a  left join  t_cl_goods b on a.goods_id=b.goods_id  " +
            "left join  t_cl_stockInfo c on a.goods_id=c.goods_id  " +
            "where a.shop_id =#{shop_id}   " +
            "<if test=' warehouse_id!=0'> and a.warehouse_id= #{warehouse_id} </if>" +
            "</script>")
    List<Map<String, Object>> stockTotalList(Integer shop_id, Integer warehouse_id);

    /**
     * 库存不足预警
     *
     * @param shop_id
     * @param warehouse_id
     * @return
     */
    @Select("<script>" +
            "select b.goods_id,b.goods_name,b.goods_num,b.goods_format,b.goods_unit,a.goods_number,a.batch_number,a.production_date,a.expiration,b.goods_warning ,c.warehouse_name,b.UDI " +
            "from t_cl_goodsStock  a " +
            "left join t_cl_goods b on a.goods_id = b.goods_id " +
            "left join t_cl_warehouse c on a.warehouse_id = c.warehouse_id where a.goods_number &lt;=b.goods_warning " +
            "and a.shop_id = #{shop_id} " +
            "<if test=' warehouse_id!=0'> and a.warehouse_id= #{warehouse_id} </if> " +
            "</script>")
    List<Map<String, Object>> listStockWarning(Integer shop_id, Integer warehouse_id);

    /**
     * 更新入库仓库id
     *
     * @param warehouse_id
     * @param stock_num
     */
    @Update("update t_cl_stockInfo set warehouse_id  = #{warehouse_id} where stock_num = #{stock_num}")
    void updateStockInfoWarehouse(Integer warehouse_id, String stock_num);
}
